package com.lsw.jgyf.ui.activity;


import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;

import com.lsw.jgyf.R;
import com.lsw.jgyf.ui.base.activity.BaseActivity;
import com.lsw.jgyf.ui.base.activity.TitleBarActivity;
import com.lsw.jgyf.ui.fragment.HomeFragment1;
import com.lsw.jgyf.ui.fragment.HomeFragment2;
import com.lsw.jgyf.ui.fragment.HomeFragment3;
import com.lsw.jgyf.ui.fragment.HomeFragment4;
import com.lsw.shareUser.ShareUserInfoUtil;
import com.lsw.util.AppManager;
import com.lsw.util.ToastUtil;
import com.lsw.view.TabRadioButton;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private TabRadioButton radio_home1, radio_home2, radio_home3, radio_home4;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    //首页
    private HomeFragment1 homeFragment;
    private HomeFragment2 homeFragment1;
    private HomeFragment3 homeFragment2;
    private HomeFragment4 homeFragment3;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initEventAndData() {
        radio_home1 = findViewById(R.id.radio_home1);
        radio_home2 = findViewById(R.id.radio_home2);
        radio_home3 = findViewById(R.id.radio_home3);
        radio_home4 = findViewById(R.id.radio_home4);
        radio_home1.setOnClickListener(this);
        radio_home2.setOnClickListener(this);
        radio_home3.setOnClickListener(this);
        radio_home4.setOnClickListener(this);

        homeFragment = new HomeFragment1();
        homeFragment1 = new HomeFragment2();
        homeFragment2 = new HomeFragment3();
        homeFragment3 = new HomeFragment4();
        showFragment(0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.radio_home1:
                showFragment(0);
                break;
            case R.id.radio_home2:
//                if (ShareUserInfoUtil.getInstance(context).getString(ShareUserInfoUtil.USER_TOKEN, "").equals("")) {
//                    openLogin(false);
//                    radio_home1.setChecked(true);
//                    showFragment(0);
//                } else
                showFragment(1);
                break;
            case R.id.radio_home3:
//                if (ShareUserInfoUtil.getInstance(mContext).getString(ShareUserInfoUtil.USER_TOKEN, "").equals("")) {
//                    openLogin(false);
//                    radio_home1.setChecked(true);
//                    showFragment(0);
//                } else
                showFragment(2);
                break;
            case R.id.radio_home4:
                if (ShareUserInfoUtil.getInstance(mContext).getString(ShareUserInfoUtil.USER_TOKEN, "").equals("")) {
                    openLogin(false);
                    radio_home1.setChecked(true);
                    showFragment(0);
                } else
                    showFragment(3);
                break;
        }
    }


    public void showFragment(int position) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        switch (position) {
            case 0:
                radio_home1.setChecked(true);
                if (!homeFragment.isAdded()) {
                    fragmentTransaction.add(R.id.fragment, homeFragment);
                }
                fragmentTransaction.show(homeFragment);
                fragmentTransaction.hide(homeFragment1);
                fragmentTransaction.hide(homeFragment2);
                fragmentTransaction.hide(homeFragment3);

                fragmentTransaction.commitAllowingStateLoss();

                break;
            case 1:
                radio_home2.setChecked(true);
                if (!homeFragment1.isAdded()) {
                    fragmentTransaction.add(R.id.fragment, homeFragment1);
                }
                fragmentTransaction.hide(homeFragment);
                fragmentTransaction.show(homeFragment1);
                fragmentTransaction.hide(homeFragment2);
                fragmentTransaction.hide(homeFragment3);

                fragmentTransaction.commitAllowingStateLoss();

                break;
            case 2:
                radio_home3.setChecked(true);
                if (!homeFragment2.isAdded()) {
                    fragmentTransaction.add(R.id.fragment, homeFragment2);
                }
                fragmentTransaction.hide(homeFragment);
                fragmentTransaction.hide(homeFragment1);
                fragmentTransaction.show(homeFragment2);
                fragmentTransaction.hide(homeFragment3);
                fragmentTransaction.commitAllowingStateLoss();
                break;
            case 3:
                radio_home4.setChecked(true);
                if (!homeFragment3.isAdded()) {
                    fragmentTransaction.add(R.id.fragment, homeFragment3);
                }
                fragmentTransaction.hide(homeFragment);
                fragmentTransaction.hide(homeFragment1);
                fragmentTransaction.hide(homeFragment2);
                fragmentTransaction.show(homeFragment3);
                fragmentTransaction.commitAllowingStateLoss();
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (homeFragment3 != null)
            homeFragment3.onActivityResult(requestCode, resultCode, data);
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // 点击一次返回按钮
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtil.showToast(mContext, "再按一次，退出应用");
                exitTime = System.currentTimeMillis();
            } else {
                AppManager.getAppManager().AppExit(mContext);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
